//
//  CZTabBarController.m
//  OCKSFWeibo
//
//  Created by 盛 发 况 on 16/4/16.
//  Copyright © 2016年 盛 发 况. All rights reserved.
//

#import "CZTabBarController.h"
#import "CZTabBar.h"
#import "CZNavigationController.h"

#import "CZHomeViewController.h"
#import "CZMessageViewController.h"
#import "CZDiscoverViewController.h"
#import "CZProfileViewController.h"

@interface CZTabBarController ()<CZTabBarDelegate>
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation CZTabBarController
- (NSMutableArray *)items {
    if (_items == nil) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /** 添加所有的子控制器*/
    [self setUpAllChildViewController];
    
    // 自定义tabBar
    [self setUpTabBar];
    
}

#pragma mark - 设置tabBar
- (void)setUpTabBar
{
    // 自定义tabBar
    CZTabBar *tabBar = [[CZTabBar alloc] initWithFrame:self.tabBar.frame];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    tabBar.delegate = self;
    tabBar.items = self.items;
    [self.view addSubview:tabBar];
    // 移除系统的tabBar
    [self.tabBar removeFromSuperview];
}


#pragma mark - 当点击tabBar上的按钮调用,代理方法实现
- (void)tabBar:(CZTabBar *)tabBar didClickButton:(NSInteger)index {
    
    self.selectedIndex = index;
}
//隐藏状态栏
//- (BOOL)prefersStatusBarHidden {
//    
//    return YES;
//}
#pragma mark  添加所有的子控制器
- (void)setUpAllChildViewController {
    
    //首页
    CZHomeViewController *home = [[CZHomeViewController alloc] init];
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"];
    
    //消息
    CZMessageViewController *message = [[CZMessageViewController alloc] init];
    [self setUpOneChildViewController:message image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] title:@"消息"];
   
    //发现
    CZDiscoverViewController *discover = [[CZDiscoverViewController alloc] init];
    [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"];
    
    //我
    CZProfileViewController *profile = [[CZProfileViewController alloc] init];
    [self setUpOneChildViewController:profile image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"我"];

    
}

#pragma mark - 添加一个子控制器
- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title
{
    
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    vc.title = title;
    vc.tabBarItem.badgeValue = @"10";
    
    // 保存tabBarItem模型到数组
    [self.items addObject:vc.tabBarItem];
    
    CZNavigationController *nav = [[CZNavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}

@end


